package com.gxuwz.medical.domain.policy;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DbUtil;

/**
 * 慢性病政策设置
 * 
 * @author 演示
 *
 */
public class Policy {
	private String id;
	private String year;// 执行年度
	private String rate;// 比例
	private String max_line;// 封顶线

	public Policy() {
		super();
	}

	/**
	 * 根据年份查询慢性表政策信息
	 * 
	 * @param year
	 * @throws SQLException
	 */
	public Policy(String year) throws SQLException {
		if (year == null) {
			throw new IllegalStateException("家庭档案信息编号不能空");
		}
		try {
			this.year = year;
			loadDB();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据年份查询慢性表政策信息
	 * 
	 * @param year
	 * @throws SQLException
	 */
	private void loadDB() throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn.prepareStatement("select * from t_policy where year=?");
			int index = 1;
			pstmt.setString(index, this.getYear());
			rs = pstmt.executeQuery();
			if (rs.next()) {

				this.year = rs.getString("year");
				this.rate = rs.getString("rate");
				this.max_line = rs.getString("max_line");
			}
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}

	public void addPolicy(String year, String rate, String max_line) throws Exception {
		Connection conn = null;
		try {
			// 初始化属性
			this.year = year;
			this.rate = rate;
			this.max_line = max_line;

			// 保存导数据库
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			// 调用插入数据库私有方法
			this.saveToDB(conn);
			conn.commit();
		} catch (Exception e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	/**
	 * 插入记录
	 * 
	 * @param connection
	 * @throws SQLException
	 */
	private void saveToDB(Connection connection) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into t_policy(year,rate,max_line)");
			sqlBuff.append("values(?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = connection.prepareStatement(sql);
			// 依次设置动态参数的值，注意顺序
			int index = 1;
			pstmt.setString(index++, this.year);// 行政区域编码
			pstmt.setString(index++, this.rate);
			pstmt.setString(index++, this.max_line);

			// 以executeUpdate()执行SQL
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			// 只关闭PrepareStatement资源
			if (pstmt != null) {
				pstmt.close();
			}

		}

	}

	public void del(String id) throws SQLException {
		System.out.println("del=" + id);
		Connection conn = null;
		try {
			// 1:对象属性赋值
			this.id = id;
			conn = DbUtil.getConn();
			// 2；调用delFromDB方法
			delFromDB(conn);
		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(conn);
		}

	}

	private void delFromDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			System.out.println("delDB=" + id);
			// 定义构造插入SQL语句字符串变量sql
			String sql = "delete from t_policy where id=?";
			// 创建执行带动态参数的SQL的PreparedStatement pstmt
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数对应的值
			int index = 1;
			pstmt.setString(index++, this.id);

			int count = pstmt.executeUpdate();

		} catch (SQLException e) {
			throw e;
		} finally {
			DbUtil.close(pstmt);

		}

	}

	public Policy(String year, String rate, String max_line) {
		super();
		this.year = year;
		this.rate = rate;
		this.max_line = max_line;
	}

	public Policy(String id, String year, String rate, String max_line) {
		super();
		this.id = id;
		this.year = year;
		this.rate = rate;
		this.max_line = max_line;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getRate() {
		return rate;
	}

	public void setRate(String rate) {
		this.rate = rate;
	}

	public String getMax_line() {
		return max_line;
	}

	public void setMax_line(String max_line) {
		this.max_line = max_line;
	}

}
